package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.Permission;
import com.hotelManagerSystem.model.Role;
import com.hotelManagerSystem.model.RolePermission;
import com.hotelManagerSystem.service.IPermissionService;
import com.hotelManagerSystem.service.IRolePermissionService;
import com.hotelManagerSystem.service.IRoleService;
import com.hotelManagerSystem.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  分配权限 前端控制器
 * </p>
 *
 * @author longyajr
 * @since 2021-12-22
 */

@Controller
@RequestMapping("/role-permission")
public class RolePermissionController {
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IPermissionService iPermissionService;
    @Autowired
    private IRolePermissionService iRolePermissionService;
    @Autowired
    private RoleController roleController;

//    修改角色权限页面跳转
    @RequestMapping("/FPPermissionUI")
    public String FPPermissionUI(Model model, int roleId,
                                 @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                 @RequestParam(defaultValue="8",value="pageSize")Integer pageSize,
                                 @RequestParam(required = false,value="PermissionName")String PermissionName
    ){
//        System.out.println("------------FPPermissionUI---------------");
        PageUtil.runStartPage(pageNum,pageSize);
        Role role_db = iRoleService.getById(roleId);
        List<Permission> permission_dbList = iPermissionService.list();
        if(PermissionName == null){
            permission_dbList = iPermissionService.list();
        }else{
            permission_dbList = iPermissionService.list();
        }
//        分页对象
        PageInfo pageInfo = PageUtil.getPageInfo(permission_dbList);
        model.addAttribute("role_db",role_db);
        model.addAttribute("permission_dbList",permission_dbList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("PermissionName",PermissionName);

        return "/admin/permissionPage/FPPermission";
    }

//    修改角色权限
    @RequestMapping("/FPPermission")
    public String FPPermission(Model model, RolePermission rolePermission,String roleIdStr, String[] permissionIdStrs){
//        System.out.println("------------FPPermission---------------");
        int roleId = Integer.parseInt(roleIdStr);
//        查找数据库是否有这个角色的权限信息，有就刪
        boolean m = iRolePermissionService.deleteRolePermissionByRoleId(roleId);

        for(String permissionIdStr:permissionIdStrs){
            int permissionId = Integer.parseInt(permissionIdStr);
            System.out.println(permissionId);
//            装配role-permission
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            boolean b = iRolePermissionService.save(rolePermission);
        }

//        跳转回角色列表页面
//        return list(model);

//        List<Role> role_dbList = iRoleService.queryRoleList();
//        model.addAttribute("role_dbList",role_dbList);

        return roleController.list(model,1,8,"");
    }

}

